import { SysMenuApi } from '/@/api/sysApi/SysMenuApi.ts'
import { RouterUtil } from '/@/utils/RouterUtil.ts'
import { Menu } from '/@/module/Menu.ts'
import { CommonStorage } from '/@/storage/CommonStorage.ts'
import router from '/@/router/Router.ts'

export namespace Auth {
  /**
   * 重新设置后端路由，包括查询菜单树，处理菜单树以及添加路由。此函数为异步
   */
  export const resetMenuAndRouter = async (): Promise<Menu> => {
    const {
      data: { data: menuTree },
    } = await SysMenuApi.forest()
    const menu: Menu = new Menu(menuTree)
    CommonStorage.instance.set('menu', menu)
    RouterUtil.addRouterList(menu.getRouteTree)
    console.log('后端路由设置完毕：', router.getRoutes())
    return menu
  }
}
